//=============================================================
// 创建人:            ssd
// 创建时间:          8/15/2024 9:19:30 PM
// 邮箱：             1292934053@qq.com
//==============================================================
using LF.Common;
using LF.Domain.Entities;
using LF.Domain.Helper;
using LF.Shared.Dtos.Input;
using System.Linq.Expressions;
using static LF.Common.AppConfig;

namespace LF.Domain.Services.Iml
{
    public class RoleManagerService : IRoleManagerService
    {
        public List<Role> All(int index, int pageSize, RoleSearch search)
        {
            var query = FreeSqlHelper.DbContext.Queryable<Role>();
            if (!search.Name.IsNullOrWhiteSpace())
            {
                query = query.Where(p => p.Name.Contains(search.Name!));
            }
            if (search.Status.HasValue)
            {
                query = query.Where(p => p.Status == search.Status.Value);
            }
            if (!search.Remark.IsNullOrWhiteSpace())
            {
                query = query.Where(p => p.Remark.Contains(search.Remark!));
            }
            var list = query
                .OrderByDescending(p => p.UpdateTime)
                .Page(index, pageSize)
                .ToList();
            return list;
        }

        public long Count(RoleSearch search)
        {
            var query = FreeSqlHelper.DbContext.Queryable<Role>();
            if (!search.Name.IsNullOrWhiteSpace())
            {
                query = query.Where(p => p.Name.Contains(search.Name!));
            }
            if (search.Status.HasValue)
            {
                query = query.Where(p => p.Status == search.Status.Value);
            }
            if (!search.Remark.IsNullOrWhiteSpace())
            {
                query = query.Where(p => p.Remark.Contains(search.Remark!));
            }
            var count = query.Count();
            return count;
        }

        public Role Get(Guid id)
        {
            var item = FreeSqlHelper.DbContext.Queryable<Role>().Where(p => p.Id == id).First();
            return item;
        }

        public bool RealDelete(Guid id)
        {
            var result = FreeSqlHelper.DbContext.Delete<Role>(id).ExecuteAffrows();
            return result > 0;
        }

        public bool Insert(Role role)
        {
            var result = FreeSqlHelper.DbContext.Insert(role).ExecuteAffrows();
            return result > 0;
        }
      
        public bool Update(Role role)
        {
            var result = FreeSqlHelper.DbContext.Update<Role>(role.Id)
               .SetSource(role)
               .ExecuteAffrows();
            return result > 0;
        }

        public bool Inserts(List<RoleMenu> roleMenu)
        {
            var result = FreeSqlHelper.DbContext.Insert(roleMenu).ExecuteAffrows();
            return result > 0;
        }

        public bool ClearMenus(Guid id)
        {
            var result = FreeSqlHelper.DbContext.Delete<RoleMenu>(new { RoleId = id }).ExecuteAffrows();
            return result > 0;
        }

        public List<RoleMenu> Meuns(Guid id)
        {
            var list = FreeSqlHelper.DbContext.Queryable<RoleMenu>().Where(p => p.RoleId == id).ToList();
            return list;
        }

        public List<Role> All()
        {
            var query = FreeSqlHelper.DbContext.Queryable<Role>();
            var list = query
                .OrderBy(p => p.Name)
                .ToList();
            return list;
        }
    }
}

